package com.lihongrui.cate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lihongrui.cate.common.R;
import com.lihongrui.cate.entity.Category;
import com.lihongrui.cate.entity.Setmeal;
import com.lihongrui.cate.entity.dto.SetmealDTO;
import com.lihongrui.cate.service.CategoryService;
import com.lihongrui.cate.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lihongrui
 * @create 2022-12-11 10:40
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page<SetmealDTO>> pageSetmeal(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam(value = "name", required = false) String name) {
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        Wrappers.<Setmeal>lambdaQuery().like(ObjectUtils.isNotEmpty(name), Setmeal::getName, name);
        setmealService.page(page1);
        Page<SetmealDTO> page2 = new Page<>();
        //对象拷贝
        BeanUtils.copyProperties(page1, page2, "records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDTO> collect = records.stream().map(s -> {
            SetmealDTO setmealDTO = new SetmealDTO();
            BeanUtils.copyProperties(s, setmealDTO);
            Category byId = categoryService.getById(s.getCategoryId());
            if(byId != null){
                setmealDTO.setCategoryName(byId.getName());
            }
            return setmealDTO;
        }).collect(Collectors.toList());
        page2.setRecords(collect);
        return R.success(page2);
    }

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存
    public R<String> save(@RequestBody SetmealDTO setmealDto){
        log.info("套餐信息：{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }



}
